package com.kang.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.ItemDetail;
import com.kang.pojo.StorageDistribute;
import com.kang.pojo.Storagedistribte_iteminformation;
import com.kang.pojo.Warehouse;
import com.kang.service.StorageDistributeService;
import com.kang.service.UserService;
import com.kang.service.WarehouseService;
import com.vonechina.entity.page.ItemDetailPage;
import com.vonechina.entity.page.StorageDistributePage;

/**
 * 库存调配
 * 
 * @author ysq
 *
 */
@Controller
@RequestMapping("storageDistribute")
@SessionAttributes({ "storageDistributePage" })
public class StorageDistributeController extends com.kang.controller.Controller {
	/**
	 * 用户服务
	 */
	@Resource
	private UserService users;
	/**
	 * 仓库服务
	 */
	@Resource
	private WarehouseService warehouseService;
	/**
	 * 库存调配服务
	 */
	@Resource
	private StorageDistributeService storageDistributeService;

	/**
	 * 根据storageDistributeID删除storagedistribute
	 * 
	 * @param storageDistributeID
	 * @return
	 */
	@RequestMapping("delStorageDistribute")
	@ResponseBody
	public String delStorageDistribute(Integer storageDistributeID) {
		AjaxResult result = new AjaxResult();
		// 根据storageDistribteID删除storagedistribute
		int a = storageDistributeService
				.deleteStoragedistribute(storageDistributeID);

		// 根据storageDistribteID删除所有storagedistribte_iteminformation信息
		int b = storageDistributeService
				.delStoragedistribteIteminformation(storageDistributeID);

		if (a > 0 && b > 0) {
			result.setResult("success");
		} else {
			result.setResult("fail");
		}

		return JSON.toJSONString(result);
	}

	/**
	 * 去到查看库存调配详情
	 * 
	 * @param model
	 * @param storageDistributeID
	 * @return
	 */
	@RequestMapping("toDetailsStorageDistribute")
	public String toDetailsStorageDistribute(Model model,
			Integer storageDistributeID) {
		// 根据storageDistributeID查询库存调配详情
		StorageDistribute storageDistribute = storageDistributeService
				.selectStoragedistributeByStorageDistributeID(storageDistributeID);
		model.addAttribute("storageDistribute", storageDistribute);

		// 根据storageDistributeID查询库存调配详情_物品信息
		List<Map<?, ?>> list1 = storageDistributeService
				.selectStoragedistribteIteminformationByStorageDistributeID(storageDistributeID);

		model.addAttribute("list2", list1);

		model.addAttribute("user2",
				users.getUserById(storageDistribute.getCreatePersonID() + "")
						.get(0));// 根据用户id查询出用户信息和关联的角色信息

		model.addAttribute("user3",
				users.getUserById(storageDistribute.getCommitPersonID() + "")
						.get(0));// 根据用户id查询出用户信息和关联的角色信息

		model.addAttribute(
				"user4",
				users.getUserById(storageDistribute.getLastPesonID() + "").get(
						0));// 根据用户id查询出用户信息和关联的角色信息

		model.addAttribute("warehouseList", warehouseService.listAllWarehouse());// 查询仓库
		return "stock/storageDistribute/detailsStorageDistribute";
	}

	/**
	 * 去到修改库存调配
	 * 
	 * @param session
	 * @param model
	 * @param storageDistributeID
	 * @return
	 */
	@RequestMapping("toUpdate")
	public String toUpdate(HttpSession session, Model model,
			Integer storageDistributeID) {
		initRight(session);
		if (checkRight("编辑全部库存调配") != SUCCESS) {
			return "basic/noRight";
		}

		// 根据storageDistributeID查询库存调配详情
		StorageDistribute storageDistribute = storageDistributeService
				.selectStoragedistributeByStorageDistributeID(storageDistributeID);
		model.addAttribute("storageDistribute", storageDistribute);

		// 根据storageDistributeID查询库存调配详情_物品信息
		List<Map<?, ?>> list1 = storageDistributeService
				.selectStoragedistribteIteminformationByStorageDistributeID(storageDistributeID);
		model.addAttribute("list2", list1);

		model.addAttribute("user2",
				users.getUserById(storageDistribute.getCreatePersonID() + "")
						.get(0));// 根据用户id查询出用户信息和关联的角色信息

		model.addAttribute("user3",
				users.getUserById(storageDistribute.getCommitPersonID() + "")
						.get(0));// 根据用户id查询出用户信息和关联的角色信息

		model.addAttribute(
				"user4",
				users.getUserById(storageDistribute.getLastPesonID() + "").get(
						0));// 根据用户id查询出用户信息和关联的角色信息

		model.addAttribute("warehouseList", warehouseService.listAllWarehouse());// 查询仓库

		return "stock/storageDistribute/updateStorageDistribute";
	}

	/**
	 * 修改库存调配
	 * 
	 * @param StorageDistribute
	 * @param itemIds
	 * @param itemNumber
	 * @param intemInformationID
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public String update(StorageDistribute StorageDistribute,
			Integer[] itemIds, Integer[] itemNumber,
			Integer[] intemInformationID) {
		AjaxResult result = new AjaxResult();

		// 修改领库存调配信息
		storageDistributeService.updateStorageDistribute(StorageDistribute);
		// 根据storageDistribteID删除所有storagedistribte_iteminformation信息
		storageDistributeService
				.delStoragedistribteIteminformation(StorageDistribute
						.getStorageDistributeID());

		for (int i = 0; i < itemIds.length; i++) {
			// 库存调配详情
			Storagedistribte_iteminformation storagedistribte_iteminformation = new Storagedistribte_iteminformation();
			storagedistribte_iteminformation
					.setStorageDistribteID(StorageDistribute
							.getStorageDistributeID());
			storagedistribte_iteminformation
					.setIteminformationID(intemInformationID[i]);
			storagedistribte_iteminformation.setFromSourceID(itemIds[i]);
			storagedistribte_iteminformation.setNumber(itemNumber[i]);

			// 如果是提交状态那就提交以及更改库存
			if (StorageDistribute.getStatus().equals(1)) {
				// 根据id查询出item_detail
				ItemDetail item_detail1 = storageDistributeService
						.selectItem_detailById(itemIds[i]);
				item_detail1.setNumber(itemNumber[i]);
				item_detail1.setWarehouseManagementID(StorageDistribute
						.getToWarehouseID());
				Integer item_detailId = item_detail1.getId();

				// 根据批次号和仓库id查询出数据库是否已经存在数据，存在则修改，否则添加
				ItemDetail item_detail2 = storageDistributeService
						.selectItem_detailByCondition(
								item_detail1.getBatchNumber(),
								StorageDistribute.getToWarehouseID());

				if (item_detail2 != null && item_detail2.getId() != 0) {
					// 修改item_detail数量
					storageDistributeService.updateItem_detailNumber(
							item_detail2.getId(), itemNumber[i]);
					// 设置提交后新增的批次id
					storagedistribte_iteminformation.setToSrouceID(item_detail2
							.getId());

				} else {
					// 添加item_detail
					storageDistributeService.addItem_detail(item_detail1);
					// 设置提交后新增的批次id
					storagedistribte_iteminformation.setToSrouceID(item_detail1
							.getId());

				}
				// 根据id修改原item_detail数量
				storageDistributeService.updateItem_detail(item_detailId,
						itemNumber[i]);

			}
			// 保存库存调配详情
			storageDistributeService
					.addStoragedistribte_iteminformation(storagedistribte_iteminformation);
		}

		result.setResult("success");
		return JSON.toJSONString(result);
	}

	/**
	 * 添加库存调配-获取item_Detail表
	 * 
	 * @param itemDetailPage
	 * @return
	 */
	@RequestMapping("listAddItem")
	@ResponseBody
	public String listAddItem(ItemDetailPage itemDetailPage) {
		itemDetailPage.setPageSize(10);// 设置每页显示几条
		List<Map<?, ?>> pickinglist1 = storageDistributeService
				.selectItemDetailList(itemDetailPage);// 查询数据
		AjaxResult result = new AjaxResult();
		result.setRows(pickinglist1);
		result.setTotal(itemDetailPage.getRows());
		return JSON.toJSONString(result);
	}

	/**
	 * 去到添加领料归还
	 * 
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("toAdd")
	public String addGeneralStorage(HttpSession session, Model model) {
		initRight(session);
		if (checkRight("新增库存调配") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("warehouseList", warehouseService.listAllWarehouse());// 查询可用仓库
		return "stock/storageDistribute/addStorageDistribute";
	}

	/**
	 * 添加库存调配
	 * 
	 * @param StorageDistribute
	 * @param itemIds
	 * @param itemNumber
	 * @param intemInformationID
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addWarehouseOrders(StorageDistribute StorageDistribute,
			Integer[] itemIds, Integer[] itemNumber,
			Integer[] intemInformationID) {
		AjaxResult result = new AjaxResult();
		// 生成库存调配单号
		StorageDistribute.setDistributeNumber(storageDistributeService
				.generateDistributeNumber());
		// 添加库存调配信息
		storageDistributeService.addStorageDistribute(StorageDistribute);

		if (StorageDistribute.getStorageDistributeID() == null
				|| StorageDistribute.getStorageDistributeID() <= 0) {
			return JSON.toJSONString(result);
		}

		for (int i = 0; i < itemIds.length; i++) {
			// 库存调配详情
			Storagedistribte_iteminformation storagedistribte_iteminformation = new Storagedistribte_iteminformation();
			storagedistribte_iteminformation
					.setStorageDistribteID(StorageDistribute
							.getStorageDistributeID());
			storagedistribte_iteminformation
					.setIteminformationID(intemInformationID[i]);
			storagedistribte_iteminformation.setFromSourceID(itemIds[i]);
			storagedistribte_iteminformation.setNumber(itemNumber[i]);

			// 如果是提交状态那就提交以及更改库存
			if (StorageDistribute.getStatus().equals(1)) {
				// 根据id查询出item_detail
				ItemDetail item_detail1 = storageDistributeService
						.selectItem_detailById(itemIds[i]);
				item_detail1.setNumber(itemNumber[i]);
				item_detail1.setWarehouseManagementID(StorageDistribute
						.getToWarehouseID());
				Integer item_detailId = item_detail1.getId();

				// 根据批次号和仓库id查询出数据库是否已经存在数据，存在则修改，否则添加
				ItemDetail item_detail2 = storageDistributeService
						.selectItem_detailByCondition(
								item_detail1.getBatchNumber(),
								StorageDistribute.getToWarehouseID());

				if (item_detail2 != null && item_detail2.getId() != 0) {
					// 修改item_detail数量
					storageDistributeService.updateItem_detailNumber(
							item_detail2.getId(), itemNumber[i]);
					// 设置提交后新增的批次id
					storagedistribte_iteminformation.setToSrouceID(item_detail2
							.getId());

				} else {
					// 添加item_detail
					storageDistributeService.addItem_detail(item_detail1);
					// 设置提交后新增的批次id
					storagedistribte_iteminformation.setToSrouceID(item_detail1
							.getId());

				}
				// 根据id修改原item_detail数量
				storageDistributeService.updateItem_detail(item_detailId,
						itemNumber[i]);

			}
			// 保存库存调配详情
			storageDistributeService
					.addStoragedistribte_iteminformation(storagedistribte_iteminformation);
		}

		result.setResult("success");
		return JSON.toJSONString(result);
	}

	/**
	 * 查询库存调配
	 * 
	 * @param session
	 * @param model
	 * @param storageDistributePage
	 * @return
	 */
	@RequestMapping("selectByWhere")
	public String storageDistributeSelectByWhere(HttpSession session,
			Model model, StorageDistributePage storageDistributePage) {
		initRight(session);
		if (checkRight("查看库存调配") != SUCCESS
				&& checkRight("查看全部库存调配") != SUCCESS) {
			return "basic/noRight";
		}
		if (checkRight("查看全部库存调配") == SUCCESS) {
			storageDistributePage.setCreatePersonID(null);
		} else {
			storageDistributePage.setCreatePersonID(userID + "");
		}

		List<Map<?, ?>> storageDistributeList = storageDistributeService
				.selectByWheres(storageDistributePage);// 查询库存调配

		model.addAttribute("storageDistributeList", storageDistributeList);

		List<Warehouse> wm = warehouseService.listAllWarehouse();// 仓库
		model.addAttribute("wm", wm);

		storageDistributePage.setPageUrl("storageDistribute/selectByWhere.do");
		model.addAttribute("page", storageDistributePage);
		return "stock/storageDistribute/listStorageDistribute";
	}
}
